Skip to content

Let a freestanding object compare text - #37

Merged
bytetroll merged 1 commit into
masterfrom
freestanding-text-compare
Nov 13, 2026
Merged

bytetroll merged 1 commit into
masterfrom
freestanding-text-compare

Conversation

@bytetroll

Copy link
Copy Markdown
Contributor

Found from the consumer side, which is the only place it could have been found: an Anomalous vessel wanted to recognise a word in a line its kernel delivered, and the object would not link. == on text emitted a call to strcmp.

That is the same defect the concatenation helper had before the last branch — it imported strlen, memcpy and calloc unconditionally — in a different operation. The allocation path was routed through the native runtime and the comparison path was not, so a freestanding object could concatenate but could not compare, and the failure was not a diagnostic: it was a libc import that a consumer's own symbol check rejects at link time, a long way from where the mistake is.

What changed

Three sites emitted strcmp: the text comparison operators, a variant tag against one expected string, and a variant tag against an allowed set. All three now ask one function for the symbol, so a fourth site cannot be routed correctly in two places and wrongly in a third. The locals are renamed with it, because a reader seeing strcmp_fn would reasonably assume libc.

The hook is nyrt_str_compare, and its contract is deliberately narrower than strcmp's: only the sign is defined, because that is all generated code reads. Native text is NUL-free, so unsigned-byte order matches the interpreter's. A null is not a text value, and the runtime orders a null before any non-null rather than reading through one, which keeps the comparison total for inputs generated code should never produce.

It is defined in the hosted runtime too, for the same reason nyrt_mem_copy and nyrt_str_length are: otherwise an object emitted with --freestanding could only ever link against the freestanding archive, and which runtime to link would stop being the host's choice.

Comparison allocates nothing, so this is a fourth conditional hook rather than a fourth allocation hook. Five always, three if the program allocates, one if it compares text — each required only by the construct that uses it, so the import surface still states what a program does rather than what the language can do.

Measured, not asserted

The same source, emitted both ways:

hosted:        nyrt_abi_version nyrt_context_begin nyrt_context_end
               nyrt_fail nyrt_profile_site probe_mode strcmp
freestanding:  nyrt_abi_version nyrt_context_begin nyrt_context_end
               nyrt_fail nyrt_profile_site nyrt_str_compare probe_mode

Then linked against the archive and run, because an import list cannot show that the answers are right — a comparison that said "equal" to everything would link and satisfy any symbol check. A program comparing "alpha", "beta" and a second "alpha" returns what Ny returned, so equality, inequality and ordering all hold.

nil.native-symbol-surface goes from 18 checks to 23 and nil.freestanding-runtime from 12 to 14. Full suite: 574 of 574 pass, re-run on freshly built binaries after the rename (3 environmental skips: PostgreSQL ×2, UI display).

Note for the consumer side

Anomalous cannot use this until a generation carrying it is built and that repository re-pins. Its vessel currently selects a test run through a cell the host writes, which is a workaround for this defect and can be simplified once the pin moves. Re-pinning is a deliberate act there — the pin is the evidence for a qualification — so it is not done here.

🤖 Generated with Claude Code

The allocation path was routed through the native runtime on this branch and
the comparison path was not, so a freestanding object could concatenate but
could not compare: `==` on text emitted a call to `strcmp`, which such an
object has no libc to resolve and whose presence its own import surface
forbids. That is the same defect the concatenation helper had before --
it imported strlen, memcpy and calloc unconditionally -- in a different
operation.

It was found from the consumer side, which is the only place it could have
been found. A vessel wanted to recognise a word in a line its kernel
delivered, and the import check refused the object.

Three sites emitted strcmp: the text comparison operators, a variant tag
against one expected string, and a variant tag against an allowed set. All
three now ask one function for the symbol, so a fourth site cannot be routed
correctly in two places and wrongly in a third. The locals are renamed with
it, because a reader seeing `strcmp_fn` would reasonably assume libc.

The hook is nyrt_str_compare, and its contract is narrower than strcmp's:
only the sign is defined, because that is all generated code reads. Native
text is NUL-free, so unsigned-byte order matches the interpreter's. A null is
not a text value, and the runtime orders a null before any non-null rather
than reading through one, which keeps the comparison total for inputs
generated code should never produce. It is defined in the hosted runtime too,
for the same reason mem_copy and str_length are: otherwise an object emitted
with --freestanding could only ever link against the freestanding archive,
and which runtime to link would stop being the host's choice.

Comparison allocates nothing, so this is a fourth conditional hook rather
than a fourth allocation hook. Five always, three if the program allocates,
one if it compares text -- each required only by the construct that uses it,
so the import surface still states what a program does rather than what the
language can do.

Measured both ways from one source. Hosted imports strcmp and no runtime
comparison hook; freestanding imports nyrt_str_compare and no libc name at
all. Then linked against the archive and run, because an import list cannot
show the answers are right: a comparison that said "equal" to everything
would link and satisfy any symbol check. A program comparing "alpha", "beta"
and a second "alpha" returns what Ny returned, so equality, inequality and
ordering all hold.

nil.native-symbol-surface goes from 18 checks to 23 and
nil.freestanding-runtime from 12 to 14.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@bytetroll
bytetroll merged commit a69c54a into master Nov 13, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant